<template>
    <el-dialog :model-value="visible" width="55%" custom-class="event-detail-dialog"
        @update:model-value="handleVisibleChange" @closed="handleClose">
        <div class="event-detail-content">
            <h2 class="event-title">{{ eventData?.title }}</h2>

            <div class="event-type">
                <el-tag size="default" :type="getEventTagType(eventData?.type)" effect="light">
                    {{ eventData?.type }}
                </el-tag>
            </div>

            <div class="event-image-container" v-if="eventData?.image">
                <el-image :src="eventData?.image" fit="cover" class="dialog-event-image"
                    :preview-src-list="[eventData.image]">
                    <template #error>
                        <div class="image-placeholder">
                            <el-icon>
                                <Picture />
                            </el-icon>
                            <span>图片加载失败</span>
                        </div>
                    </template>
                </el-image>
            </div>

            <div class="event-full-desc">
                {{ eventData?.description }}
            </div>

            <div class="event-time-bottom">
                <span class="time-text">{{ eventData?.time }}</span>
            </div>
        </div>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { Picture } from '@element-plus/icons-vue'

interface EventItem {
    title: string
    type: string
    time: string
    description: string
    image?: string
}

interface Props {
    event?: EventItem | null
    visible: boolean
}

interface Emits {
    (e: 'update:visible', value: boolean): void
    (e: 'close'): void
}

const props = withDefaults(defineProps<Props>(), {
    event: null,
    visible: false
})

const emit = defineEmits<Emits>()

const eventData = ref<EventItem | null>(null)

watch(() => props.event, (newEvent) => {
    eventData.value = newEvent
}, { immediate: true })

const getEventTagType = (type?: string) => {
    const typeMap: Record<string, string> = {
        '人生大事': 'danger',
        '教育': 'success',
        '事业': 'warning',
        '荣誉': 'primary'
    }
    return typeMap[type || ''] || 'info'
}

const handleVisibleChange = (value: boolean) => {
    emit('update:visible', value)
}

const handleClose = () => {
    emit('update:visible', false)
    emit('close')
}
</script>

<style scoped lang="scss">
.event-detail-content {
    padding: 20px 0;
    position: relative;
    min-height: 300px;

    /* 事件标题 */
    .event-title {
        text-align: center;
        font-size: 28px;
        color: var(--primary-color, #5a3e2f);
        margin: 0 0 20px 0;
        font-weight: bold;
        font-family: 'Ma Shan Zheng', cursive;
        letter-spacing: 2px;
    }

    /* 事件类型 */
    .event-type {
        text-align: center;
        margin-bottom: 25px;

        .el-tag {
            font-size: 16px;
            padding: 8px 16px;
            font-weight: 500;
        }
    }

    /* 事件图片 */
    .event-image-container {
        width: 100%;
        height: 250px;
        margin-bottom: 25px;
        background-color: #f0e6d6;
        border-radius: 8px;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid var(--border-color, #d6c8b8);

        .dialog-event-image,
        .image-placeholder {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;

            .el-icon {
                font-size: 48px;
                color: var(--primary-color, #5a3e2f);
                margin-bottom: 10px;
            }

            span {
                color: var(--primary-color, #5a3e2f);
                font-size: 14px;
            }
        }
    }

    /* 事件描述 */
    .event-full-desc {
        line-height: 1.8;
        color: var(--el-text-color-regular);
        white-space: pre-line;
        font-size: 16px;
        text-align: justify;
        margin-bottom: 40px;
        padding: 0 10px;
    }

    /* 时间 */
    .event-time-bottom {
        position: absolute;
        bottom: 10px;
        right: 0;
        text-align: right;

        .time-text {
            font-size: 14px;
            color: var(--el-text-color-secondary);
            font-style: italic;
        }
    }
}

.event-detail-dialog {
    --primary-color: #5a3e2f;
    --secondary-color: #8ba892;
    --bg-color: #f7f3e9;
    --border-color: #d6c8b8;

    .el-dialog {
        background-color: var(--bg-color);
        border-radius: 12px;

        .el-dialog__header {
            border-bottom: 1px solid var(--border-color);
            margin-right: 0;
            padding: 20px 20px 15px 20px;

            .el-dialog__title {
                color: var(--primary-color);
                font-family: 'Ma Shan Zheng', cursive;
                font-size: 24px;
                text-align: center;
                width: 100%;
            }
        }

        .el-dialog__body {
            padding: 0 20px 20px 20px;
        }
    }

    /* 弹窗动画 */
    .el-dialog {
        animation: dialog-fade-in 0.3s ease;
    }

    @keyframes dialog-fade-in {
        from {
            opacity: 0;
            transform: translateY(-20px) scale(0.95);
        }

        to {
            opacity: 1;
            transform: translateY(0) scale(1);
        }
    }
}
</style>